package com.leetcode2;

public class Index200numIslands {
	public int numIslands(char[][] grid) {
		int ans = 0;
		for (int i = 0; i < grid.length; i++)
			for (int j = 0; j < grid[i].length; j++) 
				if(grid[i][j] != '0') {
					ans++;
					dfs(grid,i,j);
				}
		
		return ans;
    }
	
	public void dfs(char[][] grid,int i,int j) {
		int high = grid.length,width = grid[0].length;
		if(i < 0 || j < 0 || i >= high || j >= width) return;
		
		if(grid[i][j] == '0') return;
		
		grid[i][j] = '0';
		dfs(grid,i + 1,j);
		dfs(grid,i - 1,j);
		dfs(grid,i,j + 1);
		dfs(grid,i,j - 1);
	}
}
